﻿// File: CsoGateway.Collections.Queue.js
// Version: 0.7.1.0
// Author: Pascal Dufresne
// Date: 2008-12-06
// Last update: 2009-05-25
// http://csogateway.codeplex.com
// http://csogateway.metaobjects.ca
// Copyright (C) 2010 Pascal Dufresne

/*
 * Register CsoGateway.Collections namespace. Import CsoGateway.System namespace.
 */
Type.registerNamespace("CsoGateway.Collections");
ImportNamespace(CsoGateway.System);

/*
 * CsoGateway.Collections.Queue is a queue of objects. It's interface is meant to resemble
 * that of the .NET class System.Collections.Generic.Queue<System.Object>
 * It's internal implementation is a CsoGateway.Collections.List.
 *
 * It allows the same object to be added many times.
 * Null values can be added but not undefined values.
 *
 *
 * Contructor                       'System.Collections.Generic.Queue<System.Object>' equivalent
 * ----------                       -----------------------------------------------------------
 * Queue()                          Queue()
 * Queue(<argument list>)           Queue(System.Collections.Generic.IEnumerable<System.Object>)
 *
 *
 * Methods                          'System.Collections.Generic.List<System.Object>' equivalent
 * ----------                       -----------------------------------------------------------
 * Count()                          Count
 * IsEmpty()                        -
 * Clear()                          Clear()
 * Contains(Object)                 Contains(System.Object)
 * Dequeue()                        Dequeue
 * Enqueue(Object)                  Enqueue(System.Object)
 * Peek()                           Peek()
 *
 */

ImportType(CsoGateway.Collections.List);

/*
 * Creates and instance of CsoGateway.Collections.Queue.
 * There are 2 ways to create a Queue object:
 * - Queue()
 *		Creates and empty Queue.
 *		Ref: System.Collection.Generic.Queue.Queue()
 *
 * - Queue(object1, object2, object3, ...)
 *		Creates a Queue and enqueues the objects in the parameter list in the same order. The first object
 *		to be dequeued will be the first in the paremeter list.
 *		Ref: System.Collection.Generic.Queue.Queue(System.Collections.Generic.IEnumerable<System.Object>)
 */
CsoGateway.Collections.Queue = function Queue()
{	
	this.innerList = new List();
	for(var i=0; i<arguments.length; ++i)
	{
		this.Enqueue(arguments[i]);
	}
}

/*
 * Gets the number of elements contained in the Queue.
 * Ref: System.Collection.Generic.Queue.Count 
 */
CsoGateway.Collections.Queue.prototype.Count =  function()
{
	return (this.innerList.Count());
}

/*
 * Tests whether this queue is currently empty (has no elements).
 */
CsoGateway.Collections.Queue.prototype.IsEmpty =  function()
{
	return (this.innerList.IsEmpty());
}

/*
 * Removes all items from the Queue.
 * Ref: System.Collection.Generic.Queue.Clear
 */
CsoGateway.Collections.Queue.prototype.Clear = function()
{	
	this.innerList.Clear();
}

/*
 * Determines whether the Queue contains a specific object.
 * The element can be null but cannot be undefined or an exception is thrown.
 * Ref: System.Collection.Generic.Queue.Contains
 */
CsoGateway.Collections.Queue.prototype.Contains = function(element)
{	
	return this.innerList.Contains(element);
}

/*
 * Removes and returns the object at the beginning of the Queue.
 * Ref: System.Collection.Generic.Queue.Dequeue
 */
CsoGateway.Collections.Queue.prototype.Dequeue = function()
{	
	if(this.innerList.Count() == 0)
		throw new Error('CsoGateway.Collections.Queue.Dequeue: The queue is empty.');
	return this.innerList.RemoveAt(0);
}

/*
 * Adds an object to the end of the Queue.
 * The element can be null but cannot be undefined or an exception is thrown.
 * Ref: System.Collection.Generic.Queue.Enqueue
 */
CsoGateway.Collections.Queue.prototype.Enqueue = function(element)
{	
	return this.innerList.Add(element);
}

/*
 * Returns the object at the beginning of the Queue without removing it.
 * Ref: System.Collection.Generic.Queue.Peek
 */
CsoGateway.Collections.Queue.prototype.Peek = function()
{
	if(this.innerList.Count() == 0)
		throw new Error('CsoGateway.Collections.Queue.Dequeue: The queue is empty.');
	
	return this.innerList.getItem(0);
}

CsoGateway.Collections.Queue.registerClass('CsoGateway.Collections.Queue', CsoNative, Sys.IDisposable);

/*
 * End of CsoGateway.Collections.Queue definition
 */

